package com.flute.haflute.tools;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 载入系统属性
 * 
 */
public class PropertiesFileUtil {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(PropertiesFileUtil.class);

	public static Properties loadPropertiesFile(String filename) {
		File file = new File(filename);
		java.io.InputStream in = null;

		try {

			if (!file.exists()) {
				in = ClassLoader.getSystemResourceAsStream(filename);
			} else {
				in = new java.io.BufferedInputStream(new FileInputStream(file));
			}

			if (in == null) {
				System.out.println(filename + "not found!");
				return null;
			}
			Properties properties = new Properties();
			properties.load(in);
			in.close();
			return properties;
		} catch (Exception ex) {
			return null;
		}
	}

	public static String getProperty(Properties properties, String name,
			String defaultValue) {
		String value = defaultValue;
		if (properties != null) {
			value = properties.getProperty(name, defaultValue);
		}
		try {
			value = new String(value.getBytes("ISO-8859-1"));
		} catch (Exception ex) {
			LOGGER.error("loadproperty error : " + name);
		}
		return value;
	}

}
